//index.js
//获取应用实例
var wxpay = require('../../utils/pay.js')
var app = getApp()

Page({
  data: {
    mallName:wx.getStorageSync('mallName'),
    selList:'',
    confirmOrder:{},
    curAddressData:null,//收货地址
    allCoupon:[],//所有能用的优惠券
    curCoupon:null,//当前优惠券
    hasPay: false
  },
  onShow:function(e){
    this.initShippingAddress();
    var c = wx.getStorageSync(this.data.selList);
    if(c){
      this.setData({
        curCoupon:c
      })
      wx.clearStorageSync(this.data.selList);
    }
    this.confirmOrder();
  },
  onLoad: function (e) {
    var that = this;
    var selList=e.sl;
    that.setData({selList:selList});
  },
  confirmOrder:function(e){
    wx.showLoading();
    var that=this;
    var said=0;
    if(that.data.curAddressData)
      said = that.data.curAddressData.SAId;
    var postData = {
      Uid: app.globalData.userInfo.Uid,
      SaId: said,
      SelList: that.data.selList
    }
    //初始化优惠券
    wx.showLoading();
    wx.request({
      url: app.globalData.host + '/api/order/GetValidCouponList',
      data: {
        selList: that.data.selList,
        uid: app.globalData.userInfo.Uid
      }, // 设置请求的 参数
      success: (res) => {
        wx.hideLoading();
        if (res.data.Code != 0) {
          console.log(res);
          return;
        }
        that.setData({
          allCoupon: res.data.Data,
          curCoupon:res.data.Data[0]
        });
      }
    })
    //初始化订单
    wx.request({
      url: app.globalData.host + '/api/order/confirmorderv2',
      method: 'POST',
      data: postData, // 设置请求的 参数
      success: (res) => {
        wx.hideLoading();
        if (res.data.Code != 0 & that.data.hasPay == false) {
          wx.showModal({
            title: '错误',
            content: res.data.Message,
            showCancel: false
          })
          return;
        }
        that.setData({ hasPay: true });

        //订单确认成功
        var confirm = that.processImageHost(res.data.Data);
        that.setData({
          confirmOrder: confirm
        });
      }
    })
    
  },
  processImageHost: function (cart) {
    if (cart.StoreOrderList.length > 0) {
      for (var i = 0; i < cart.StoreOrderList[0].StoreCartInfo.CartProductList.length; i++) {
        //主图
        cart.StoreOrderList[0].StoreCartInfo.CartProductList[i].OrderProductInfo.ShowImg = app.globalData.image190_190host + cart.StoreOrderList[0].StoreCartInfo.CartProductList[i].OrderProductInfo.ShowImg;
        if (cart.StoreOrderList[0].StoreCartInfo.CartProductList[i].GiftList.length > 0) {
          for (var j = 0; j < cart.StoreOrderList[0].StoreCartInfo.CartProductList[i].GiftList.length; j++) {
            //赠品图片
            cart.StoreOrderList[0].StoreCartInfo.CartProductList[i].GiftList[j].ShowImg = app.globalData.image190_190host + cart.StoreOrderList[0].StoreCartInfo.CartProductList[i].GiftList[j].ShowImg;
          }
        }
      }
      for (var i = 0; i < cart.StoreOrderList[0].StoreCartInfo.CartFullCutList.length; i++) {
        for (var j = 0; j < cart.StoreOrderList[0].StoreCartInfo.CartFullCutList[i].FullCutCartProductList.length; j++) {
          cart.StoreOrderList[0].StoreCartInfo.CartFullCutList[i].FullCutCartProductList[j].OrderProductInfo.ShowImg = app.globalData.image190_190host + cart.StoreOrderList[0].StoreCartInfo.CartFullCutList[i].FullCutCartProductList[j].OrderProductInfo.ShowImg;
        }
      }
      for (var i = 0; i < cart.StoreOrderList[0].StoreCartInfo.CartSuitList.length; i++) {
        for (var j = 0; j < cart.StoreOrderList[0].StoreCartInfo.CartSuitList[i].CartProductList.length; j++) {
          cart.StoreOrderList[0].StoreCartInfo.CartSuitList[i].CartProductList[j].OrderProductInfo.ShowImg = app.globalData.image190_190host + cart.StoreOrderList[0].StoreCartInfo.CartSuitList[i].CartProductList[j].OrderProductInfo.ShowImg;
        }
      }
    }
    return cart;

  },
  createOrder:function (e) {
    
    var that = this;
    var uid = app.globalData.userInfo.Uid // 用户登录 uid
    if(!uid){
      wx.showModal({
        title: '错误',
        content: '获取用户信息失败',
        showCancel:false
      })
      return;
    }
    if (that.data.curAddressData == null || !that.data.curAddressData.SAId){
      wx.showModal({
        title: '请填写地址信息',
        content: '',
        showCancel:false
      });
      return;
    }
    wx.showLoading();
    var couponIdList='';
    if (that.data.curCoupon){
      couponIdList = that.data.curCoupon.CouponId;
    }
    var remark = e.detail.value.remark; // 备注信息
    var postData = {
      Uid: app.globalData.userInfo.Uid,
      SaId: that.data.curAddressData.SAId,//配送地址id
      SelList: that.data.selList,//选中的购物车项键列表
      PayCreditCount: 0,//支付积分
      CouponIdList: couponIdList,//客户已经激活的优惠劵
      CouponSNList: '',//客户还未激活的优惠劵
      FullCut: that.data.confirmOrder.AllFullCut,//验证满减金额
      BuyerRemark: remark //备注
    }
    
    wx.request({
      url: app.globalData.host +'/api/order/SubmitOrderV2',
      method:'POST',
      data: postData, // 设置请求的 参数
      success: (res) =>{
        wx.hideLoading();
        console.log(res.data);
        if (res.data.Code != 0) {
          wx.showModal({
            title: '错误',
            content: res.data.Message,
            showCancel: false
          })
          return;
        }
        
        // 下单成功，跳转到支付界面
        var oid=res.data.Data;
        wxpay.wxpay(app, oid, "/pages/order-list/index");
        return;
        // 下单成功，跳转到订单管理界面
        // wx.redirectTo({
        //   url: "/pages/order-list/index?t=1"
        // });
      }
    })
  },
  initShippingAddress: function () {
    var that = this;
    wx.request({
      url: app.globalData.host +'/api/user/GetDefaultShipAddress',
      data: {
        uid:app.globalData.userInfo.Uid
      },
      success: (res) =>{
        if (res.data.Code == 0) {
          that.setData({
            curAddressData:res.data.Data
          });
        }
        
      }
    })
  },
  addAddress: function () {
    wx.navigateTo({
      url:"/pages/address-add/index?said=0"
    })
  },
  selectAddress: function () {
    wx.navigateTo({
      url:"/pages/select-address/index"
    })
  },
  selectCoupon:function(){
    wx.navigateTo({
      url: "/pages/member/coupon/index?st=1&sl="+this.data.selList
    })
  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    var that=this;
    wx.showLoading({
      title: '正在刷新',
      success:function(){
        that.confirmOrder()
      }
    })
    
    wx.stopPullDownRefresh();
  }
})
